package com.dundun.SSO.manage.impl;

import com.dundun.SSO.core.impl.BaseServiceImpl;
import com.dundun.SSO.manage.*;
import com.dundun.SSO.manage.dao.SsoPermissionDao;
import com.dundun.SSO.manage.pojo.*;
import com.dundun.SSO.manage.query.SsoPermissionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**********************************************************
 * 版权所有：搜影科技 保留所有权利
 * 创建日期: 2017/5/6 0006 14:19
 * 创建作者: gezhangkai
 * 版    本:
 * 功    能:
 * 最后修改时间:
 * 修改记录:
 ***********************************************************/
@Service
public class SsoPermissionServiceImpl extends BaseServiceImpl<SsoPermission> implements ISsoPermissionService {
    @Autowired
    private SsoPermissionDao ssoPermissionDao;

    @Autowired
    private ISsoUserPermissionRelationService ssoUserPermissionRelationService;

    @Autowired
    private ISsoGroupService ssoGroupService;

    @Autowired
    private ISsoGroupPermissionRelationService ssoGroupPermissionRelationService;

    @Autowired
    private ISsoRoleService ssoRoleService;

    @Autowired
    private ISsoRolePermissionRelationService ssoRolePermissionRelationService;

    @Override
    public List<SsoPermission> selectByPage(SsoPermissionQuery ssoPermissionQuery) {
        return ssoPermissionDao.selectByPage(ssoPermissionQuery);
    }

    @Override
    public Integer selectCountByPage(SsoPermissionQuery ssoPermissionQuery) {
        return ssoPermissionDao.selectCountByPage(ssoPermissionQuery);
    }

    @Override
    public List<SsoPermission> selectAllPermissionByUserId(Long id) {

        Set<SsoPermission> ssoPermissionSet = new HashSet<>();
        //获取用户所有的权限关联信息
        List<SsoUserPermissionRelation> ssoUserPermissionRelationList = ssoUserPermissionRelationService.selectAllPermissionRelationByUserId(id);
        //获取权限信息
        for(SsoUserPermissionRelation ssoUserPermissionRelation:ssoUserPermissionRelationList){
            SsoPermission ssoPermission = selectByPrimaryKey(ssoUserPermissionRelation.getPermissionId());
            if(null != ssoPermission){
                ssoPermissionSet.add(ssoPermission);
            }
        }

        //获取用户所用的分组信息，包括继承的父组
        List<SsoGroup> ssoGroupList = ssoGroupService.selectAllGroupByUserId(id);
        //获取所有组的所有权限关联信息
        List<SsoGroupPermissionRelation> ssoGroupPermissionRelationList = new ArrayList<>();
        for(SsoGroup ssoGroup:ssoGroupList) {
            ssoGroupPermissionRelationList.addAll(ssoGroupPermissionRelationService.selectAllPermissionRelationByGroupId(ssoGroup.getId()));
        }
        //获取权限信息
        for(SsoGroupPermissionRelation ssoGroupPermissionRelation : ssoGroupPermissionRelationList){
            SsoPermission ssoPermission = selectByPrimaryKey(ssoGroupPermissionRelation.getPermissionId());
            if(null != ssoPermission){
                ssoPermissionSet.add(ssoPermission);
            }
        }

        List<SsoRole> ssoRoleList = ssoRoleService.selectAllRoleByUserId(id);
        List<SsoRolePermissionRelation> ssoRolePermissionRelationList = new ArrayList<>();
        for(SsoRole ssoRole:ssoRoleList){
            ssoRolePermissionRelationList.addAll(ssoRolePermissionRelationService.selectAllPermissionRelationByRoleId(ssoRole.getId()));
        }
        for(SsoRolePermissionRelation ssoRolePermissionRelation : ssoRolePermissionRelationList){
            SsoPermission ssoPermission = selectByPrimaryKey(ssoRolePermissionRelation.getPermissionId());
            if(null != ssoPermission){
                ssoPermissionSet.add(ssoPermission);
            }
        }
        return new ArrayList<>(ssoPermissionSet);
    }
}
